package cn.com.dhcc.ddi.stat.service;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.com.dhcc.app.core.exception.DaoException;
import cn.com.dhcc.app.core.exception.ServiceException;
import cn.com.dhcc.app.core.handler.modulehandler.WhereCondition;
import cn.com.dhcc.app.core.service.BaseService;
import cn.com.dhcc.ddi.node.vo.DataCategory;
import cn.com.dhcc.ddi.node.vo.NodeInfo;
import cn.com.dhcc.ddi.stat.dao.DataSynDao;

/**
 *  
    * @ClassName: NodeStatService
    * @Description: 节点24小时统计service
    * @author 18315
    * @date 2016-12-26
    *
 */
@Service
public class NodeStatService extends BaseService {
	
	@Autowired
	private DataSynDao dao;
	
	/**
	 * 获取24小时接收统计数据
	 * @param vo
	 * @return
	 * @throws ServiceException
	 */
	public List<String> getRecTaskByHour(String id) throws ServiceException {
		List<NodeInfo> list;
		try {
			list = dao.getReceiveTotalByHour(id);
			
			List<String> counts = new ArrayList<String>();
			for(int i = 0; i <= 23; i++) {
				counts.add(null);
			}
			
			for(NodeInfo info: list) {
				int index = Integer.parseInt(info.getHour());
				counts.set(index, info.getSum());
			}
			
			return counts;
		} catch (DaoException e) {
			throw new ServiceException("获取24小时数据量统计", e);
		}
		
	}
	/**
	 * 获取24小时频次统计数据
	 * @param id 任务id
	 * @return 
	 * @throws ServiceException
	 */
	public List<String> getRecTotalByHour(String id) throws ServiceException {
		List<NodeInfo> list;
		try {
			list = dao.getReceiveTotalByHour(id);
			
			List<String> counts = new ArrayList<String>();
			for(int i = 0; i <= 23; i++) {
				counts.add(null);
			}
			
			for(NodeInfo info: list) {
				int index = Integer.parseInt(info.getHour());
				counts.set(index, info.getCount());
			}
			
			return counts;
		} catch (DaoException e) {
			throw new ServiceException("获取24小时频次统计", e);
		}
		
	}
	
	/**
	 * 获取24小时发送统计数据
	 * @param vo
	 * @return
	 * @throws ServiceException
	 */
	public List<String> getSendTaskByHour(String id) throws ServiceException {
		List<NodeInfo> list;
		try {
			list = dao.getSendDatasByHour(id);
			
			List<String> counts = new ArrayList<String>();
			for(int i = 0; i <= 23; i++) {
				counts.add(null);
			}
			
			for(NodeInfo info: list) {
				int index = Integer.parseInt(info.getHour());
				counts.set(index, info.getSum());
			}
			
			return counts;
		} catch (DaoException e) {
			throw new ServiceException("获取24小时数据量统计", e);
		}
		
	}
	/**
	 * 获取24小时频次统计数据
	 * @param id 任务id
	 * @return 
	 * @throws ServiceException
	 */
	public List<String> getSendTotalByHour(String id) throws ServiceException {
		List<NodeInfo> list;
		try {
			list = dao.getSendTotalByHour(id);
			
			List<String> counts = new ArrayList<String>();
			for(int i = 0; i <= 23; i++) {
				counts.add(null);
			}
			
			for(NodeInfo info: list) {
				int index = Integer.parseInt(info.getHour());
				counts.set(index, info.getCount());
			}
			
			return counts;
		} catch (DaoException e) {
			throw new ServiceException("获取24小时频次统计", e);
		}
		
	}
	
	/**
	 * 获取所有节点 
	 * @return
	 * @throws ServiceException
	 */
	public List<NodeInfo> getNodes() throws ServiceException {
		try {
			List<NodeInfo> Nodes = dao.getNodes();
			return Nodes;
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			throw new ServiceException("获取全部节点", e);
		}
		
		
	}
	
	/**
	 * 按顺序获取节点名称
	 * @return
	 * @throws ServiceException
	 */
	public List<String> getNodeNames() throws ServiceException {
		try {
			List<NodeInfo> nodes = dao.getNodes();
			List<String> names = new ArrayList<String>();
			for(NodeInfo node : nodes) {
				names.add(node.getName());
			}
			return names;
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			throw new ServiceException("获取全部节点名称", e);
		}
	}
	
	/**
	 * 按顺序获取节点id
	 * @return
	 * @throws ServiceException
	 */
	public List<String> getNodeId() throws ServiceException {
		try {
			List<NodeInfo> nodes = dao.getNodes();
			List<String> ids = new ArrayList<String>();
			for(NodeInfo node : nodes) {
				ids.add(node.getId());
			}
			return ids;
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			throw new ServiceException("获取全部节点id", e);
		}
	}
	
	/**
	 * 数据分类
	 * 
	 * @param DataCategory 
	 * @return
	 * @throws ServiceException
	 */
	public List<DataCategory> getDataCategory()
			throws ServiceException {
		try {
			return dao.getDataCategory();
		} catch (DaoException e) {
			throw new ServiceException("获取数据分类失败", e);
		}
	}
	
	/**
	 * 数据分类(获取与用户相关的数据分类)
	 * 
	 * @param DataCategory 
	 * @return
	 * @throws ServiceException
	 */
	public List<DataCategory> getUserDataCategory(WhereCondition condition) throws ServiceException {
		try {
			return dao.getUserDataCategory(condition);
		} catch (DaoException e) {
			throw new ServiceException("获取数据分类失败", e);
		}
	}
	
	/**
	 * 按顺序获取数据分类名称
	 * @return
	 * @throws ServiceException
	 */
	public List<String> getDataCategoryNames() throws ServiceException {
		try {
			List<DataCategory> dcys = dao.getDataCategory();
			List<String> names = new ArrayList<String>();
			for(DataCategory dataCategory : dcys) {
				names.add(dataCategory.getName());
			}
			return names;
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			throw new ServiceException("获取全部数据分类名称", e);
		}
	}
	
	/**
	 * 按顺序获取数据分类id
	 * @return
	 * @throws ServiceException
	 */
	public List<String> getDataCategoryId() throws ServiceException {
		try {
			List<DataCategory> dcys = dao.getDataCategory();
			List<String> ids = new ArrayList<String>();
			for(DataCategory dataCategory : dcys) {
				ids.add(dataCategory.getId());
			}
			return ids;
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			throw new ServiceException("获取全部数据分类id", e);
		}
	}

}
